import tkinter as tk


def create_scrollable_widget(parent, widget_type="canvas", **widget_kwargs):
    """
    创建带垂直滚动条的可滚动控件

    Args:
        parent: 父容器
        widget_type: 控件类型 ("canvas" 或 "text")
        **widget_kwargs: 控件的配置参数

    Returns:
        控件实例 (Canvas 或 Text)
    """
    # 创建框架容器
    frame = tk.Frame(parent)

    # 根据类型创建控件
    if widget_type.lower() == "canvas":
        widget = tk.Canvas(frame, **widget_kwargs)
    elif widget_type.lower() == "text":
        widget = tk.Text(frame, **widget_kwargs)
    else:
        raise ValueError("widget_type must be 'canvas' or 'text'")

    # 创建滚动条
    scrollbar = tk.Scrollbar(frame, orient='vertical', command=widget.yview)
    widget.configure(yscrollcommand=scrollbar.set)

    # 布局 - 滚动条在右边
    scrollbar.pack(side='right', fill='y')
    widget.pack(fill='both', expand=True)

    # 为控件添加pack/grid/place方法代理
    widget.frame = frame  # 保存frame引用
    widget.pack = frame.pack
    widget.grid = frame.grid
    widget.place = frame.place

    return widget


# 便捷函数
def create_scrollable_canvas(parent, **canvas_kwargs):
    """创建带滚动条的Canvas"""
    return create_scrollable_widget(parent, "canvas", **canvas_kwargs)


def create_scrollable_text(parent, **text_kwargs):
    """创建带滚动条的Text"""
    return create_scrollable_widget(parent, "text", **text_kwargs)
